using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Repository;
using ZR.Service.ProdManage.IProdManageService;

namespace ZR.Service.ProdManage
{
    /// <summary>
    /// 三级装箱明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IProdThreePackDetailService), ServiceLifetime = LifeTime.Transient)]
    public class ProdThreePackDetailService : BaseService<ProdThreePackDetail>, IProdThreePackDetailService
    {
        /// <summary>
        /// 查询三级装箱明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ProdThreePackDetailDto> GetPageList(ProdThreePackDetailQueryDto parm)
        {
            var predicate = QueryExp(parm);
            var response = new PagedInfo<ProdThreePackDetailDto>();
            var total = 0;
            response.PageSize = parm.PageSize;
            response.PageIndex = parm.PageNum;
            response.Result = Queryable().Where(predicate.ToExpression())
                .LeftJoin<ProdThreePack>((dtl, pack) => dtl.ThreePackId == pack.PackId)
                .OrderByPropertyNameIF(parm.Sort.IsNotEmpty(), parm.Sort, parm.SortType.Contains("desc") ? OrderByType.Desc : OrderByType.Asc)
                .Select((dtl, pack) => new ProdThreePackDetailDto
                {
                    DetailId = dtl.DetailId,
                    ThreePackId = dtl.ThreePackId,
                    ItemStatus = dtl.ItemStatus,
                    PackNum = dtl.PackNum,
                    PartCode = dtl.PartCode,
                    SupplierCode = dtl.SupplierCode,
                    PackUserId = dtl.PackUserId,
                    PackTime = dtl.PackTime,
                    PackType = dtl.PackType,
                    CheckStatus = dtl.CheckStatus,
                    CheckTime = dtl.CheckTime,
                    CheckUser = dtl.CheckUser,
                    OriTwoPackNo = dtl.OriTwoPackNo,
                    TwoPackNo = dtl.TwoPackNo,
                    ThreePackNo = pack.PackNo
                }).ToPageList(parm.PageNum, parm.PageSize, ref total);
            response.TotalNum = total;
            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="DetailId"></param>
        /// <returns></returns>
        public ProdThreePackDetail GetInfo(string DetailId)
        {
            var response = Queryable()
                .Where(x => x.DetailId == DetailId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加三级装箱明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ProdThreePackDetail AddProdThreePackDetail(ProdThreePackDetail model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改三级装箱明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateProdThreePackDetail(ProdThreePackDetail model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<ProdThreePackDetail> QueryExp(ProdThreePackDetailQueryDto parm)
        {
            var predicate = Expressionable.Create<ProdThreePackDetail>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ThreePackId), it => it.ThreePackId == parm.ThreePackId);
            predicate = predicate.AndIF(parm.BeginPackTime != null, it => it.PackTime >= parm.BeginPackTime);
            predicate = predicate.AndIF(parm.EndPackTime != null, it => it.PackTime <= parm.EndPackTime);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ItemStatus), it => it.ItemStatus == parm.ItemStatus);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PackType), it => it.PackType == parm.PackType);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.PartCode), it => it.PartCode == parm.PartCode);
            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.TwoPackNo), it => it.TwoPackNo == parm.TwoPackNo);
            return predicate;
        }
    }
}